package principal.controller;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.tree.TreePath;

import principal.view.gui.ShowAndFilterData;
import principal.view.gui.altaComponente.*;
import principal.view.gui.*;
import principal.view.gui.model.*;



public class CShowAndFilterData {
	DefaultTableModel model;
	int indice;
	final ShowAndFilterData datac;
	TreeKits tree;
	AltaKitsMain kitMain;
	
	public CShowAndFilterData(ShowAndFilterData data){
		datac=data;
		kitMain = data.getKitMain();
		
		data.ListenerSelection(new ListSelectionListener(){

//			@ Override
			public void valueChanged(ListSelectionEvent e) {
				// TODO Auto-generated method stub
				JTable table = datac.getTable();
			    int viewRow = table.getSelectedRow();
			    JTextField statusText = datac.getTextField();
                if (viewRow < 0) {
                    //Selection got filtered away.
                    statusText.setText("");
                } else {
                    int modelRow = 
                        table.convertRowIndexToModel(viewRow);
                    Integer[] ii = new Integer[]{ new Integer(viewRow), new Integer(modelRow) };
                    statusText.setText(
                        String.format("Selected Row in view: %d. " +
                            "Selected Row in model: %d.", 
                            ii));
            
                }
			}
	
		});
		
		data.listenerBtAgregar(new ActionListener() {
			
			// @ Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String nombre, desc,id;
				double precio;
				JTable tabla  = datac.getTable();
				model = (DefaultTableModel)tabla.getModel();
				indice = tabla.getSelectedRow();
								
				if(indice < 0){
					datac.alerta();
				}
				else{
					indice = tabla.convertRowIndexToModel(indice);
					nombre = (String)model.getValueAt(indice, 0);
					desc = (String)model.getValueAt(indice, 1);
					precio = ((Double)model.getValueAt(indice, 2)).doubleValue();
					id = (String) model.getValueAt(indice, 3);
					
					model.removeRow(indice);					
					
					Component tercer = datac.getKitMain().getAltaContainer().getTercerPanel().getComponent(0);
					ProductosAgregados pa = (ProductosAgregados)tercer;
					
					DefaultTableModel pmodel =(DefaultTableModel)pa.getTabla().getModel();

					//					pmodel.addRow(new Object[]{nombre,desc,precio,id});
					pmodel.addRow(new Object[]{nombre,desc,new Double (precio),id});
					
					pa.setTotal(precio);
					pa.getTotal();
				}
				
				
			}
		});
		
	data.listenerBtActualizar(new ActionListener() {
			
			// @ Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String nombre, desc,id;
				double precio;
				JTable tabla  = datac.getTable();
				
				MyTableModel model = (MyTableModel) tabla.getModel();
				//MyModelTree modelot = tree.getModel();
				model.refresca();
				JTree tree = kitMain.getTreeKits();
				MyModelTree modelTree = (MyModelTree)tree.getModel();
				modelTree.actualizar();
				modelTree.reload();
				
				//modelot.refrescaKit();
			}
		});

			
		
		data.ListenerDocument(new DocumentListener(){

		//	@ Override
			public void changedUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				newFilter(datac);
			}

		//	@ Override
			public void insertUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				newFilter(datac);
			}

		//	@ Override
			public void removeUpdate(DocumentEvent arg0) {
				// TODO Auto-generated method stub
				newFilter(datac);
			}
			
		});
		
		
		


	}
	
	 private void newFilter(ShowAndFilterData datac) {
		 JTextField filterText = datac.getFilterText();
		 
	       RowFilter rf = null;

	       //Roos2
	       //	       TableRowSorter<MyTableModel> sorter = datac.getSorter();
	       TableRowSorter sorter = datac.getSorter();

	       //================
	       //Roos
	       //De qui en adelante, los comentarios como Roos2 tienen este ORIGEN
	       //================
//	       RowFilter<MyTableModel, Object> rf = null;
//	       TableRowSorter<MyTableModel> sorter = datac.getSorter();
		 
		 
	       //If current expression doesn't parse, don't update.
	       try {
	           rf = RowFilter.regexFilter(filterText.getText(), new int[] {0});
	       } catch (java.util.regex.PatternSyntaxException e) {
	           return;
	       }
	       sorter.setRowFilter(rf);
	   }
	 
	 
	 

}
